package com.nanhai.buddhist.college.module.collegebase.controller.admin.studentrewardpunish;

import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;
import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentrewardpunish.vo.StudentRewardPunishUpdateReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentrewardpunish.vo.StudentRewardPunishCreateReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentrewardpunish.vo.StudentRewardPunishExcelVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentrewardpunish.vo.StudentRewardPunishExportReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentrewardpunish.vo.StudentRewardPunishPageReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentrewardpunish.vo.StudentRewardPunishRespVO;
import com.nanhai.buddhist.college.module.collegebase.convert.studentrewardpunish.StudentRewardPunishConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.studentrewardpunish.StudentRewardPunishDO;
import com.nanhai.buddhist.college.module.collegebase.service.studentrewardpunish.StudentRewardPunishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 奖励惩罚表")
@RestController
@RequestMapping("/collegebase/student-reward-punish")
@Validated
public class StudentRewardPunishController {

    @Resource
    private StudentRewardPunishService studentRewardPunishService;

    @PostMapping("/create")
    @ApiOperation("创建奖励惩罚表")
    @PreAuthorize("@ss.hasPermission('collegebase:student-reward-punish:create')")
    public CommonResult<String> createStudentRewardPunish(@Valid @RequestBody StudentRewardPunishCreateReqVO createReqVO) {
        return success(studentRewardPunishService.createStudentRewardPunish(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新奖励惩罚表")
    @PreAuthorize("@ss.hasPermission('collegebase:student-reward-punish:update')")
    public CommonResult<Boolean> updateStudentRewardPunish(@Valid @RequestBody StudentRewardPunishUpdateReqVO updateReqVO) {
        studentRewardPunishService.updateStudentRewardPunish(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除奖励惩罚表")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:student-reward-punish:delete')")
    public CommonResult<Boolean> deleteStudentRewardPunish(@RequestParam("id") String id) {
        studentRewardPunishService.deleteStudentRewardPunish(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得奖励惩罚表")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:student-reward-punish:query')")
    public CommonResult<StudentRewardPunishRespVO> getStudentRewardPunish(@RequestParam("id") String id) {
        StudentRewardPunishDO studentRewardPunish = studentRewardPunishService.getStudentRewardPunish(id);
        return success(StudentRewardPunishConvert.INSTANCE.convert(studentRewardPunish));
    }

    @GetMapping("/list")
    @ApiOperation("获得奖励惩罚表列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('collegebase:student-reward-punish:query')")
    public CommonResult<List<StudentRewardPunishRespVO>> getStudentRewardPunishList(@RequestParam("ids") Collection<String> ids) {
        List<StudentRewardPunishDO> list = studentRewardPunishService.getStudentRewardPunishList(ids);
        return success(StudentRewardPunishConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/byType/list")
    @ApiOperation("所有奖励惩罚表列表")
    @ApiImplicitParam(name = "type", value = "类型", required = true, example = "1024,2048", dataTypeClass = String.class)
    public CommonResult<List<StudentRewardPunishRespVO>> getStudentRewardPunishAllList(@RequestParam("type")String type) {
        List<StudentRewardPunishDO> list = studentRewardPunishService.getStudentRewardPunishAllList(type);
        return success(StudentRewardPunishConvert.INSTANCE.convertList(list));
    }



    @GetMapping("/page")
    @ApiOperation("获得奖励惩罚表分页")
    @PreAuthorize("@ss.hasPermission('collegebase:student-reward-punish:query')")
    public CommonResult<PageResult<StudentRewardPunishRespVO>> getStudentRewardPunishPage(@Valid StudentRewardPunishPageReqVO pageVO) {
        PageResult<StudentRewardPunishDO> pageResult = studentRewardPunishService.getStudentRewardPunishPage(pageVO);
        return success(StudentRewardPunishConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出奖励惩罚表 Excel")
    @PreAuthorize("@ss.hasPermission('collegebase:student-reward-punish:export')")
    @OperateLog(type = EXPORT)
    public void exportStudentRewardPunishExcel(@Valid StudentRewardPunishExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<StudentRewardPunishDO> list = studentRewardPunishService.getStudentRewardPunishList(exportReqVO);
        // 导出 Excel
        List<StudentRewardPunishExcelVO> datas = StudentRewardPunishConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "奖励惩罚表.xls", "数据", StudentRewardPunishExcelVO.class, datas);
    }

}
